using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_EquipInfoStar : UIBaseNode
    {

        #region Template Generate,don't modify
        protected class UIB_UIN_EquipInfoStar
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton buttonRight { protected set; get; }
            public UnityEngine.GameObject commonEquip { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonLeft { protected set; get; }
            public UnityEngine.GameObject attributeMould { protected set; get; }
            public UnityEngine.GameObject skillDes { protected set; get; }
            public IQIGame.Onigao.Framework.ExToggle toggleMaxStarPreview { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textCost { protected set; get; }
            public UnityEngine.GameObject costMoney { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnUpStar { protected set; get; }
            public UnityEngine.GameObject starMax { protected set; get; }
            public UnityEngine.GameObject costTip { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonRight", out var __tbv0);
                this.buttonRight = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonEquip", out var __tbv1);
                this.commonEquip = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonLeft", out var __tbv2);
                this.buttonLeft = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attributeMould", out var __tbv3);
                this.attributeMould = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("skillDes", out var __tbv4);
                this.skillDes = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExToggle>("toggleMaxStarPreview", out var __tbv5);
                this.toggleMaxStarPreview = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv6);
                this.star = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textCost", out var __tbv7);
                this.textCost = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("costMoney", out var __tbv8);
                this.costMoney = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnUpStar", out var __tbv9);
                this.btnUpStar = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("starMax", out var __tbv10);
                this.starMax = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("costTip", out var __tbv11);
                this.costTip = __tbv11;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        UIN_CommonEquip equip = null;
        ListScrollAdapter<UIN_CommonAttribute> attrs = null;
        ListScrollAdapter<UIN_CommonTitleValue> skillNameDescs = null;
        ListScrollAdapter<UIN_CommonDarkLightPreview> stars = null;
        UIN_CostMoney costMoney = null;

        UD_EquipInfo udEquip = null;
        int equipCount = 0;
        int equipIndex = 0;
        int starInit = 0;
        int starCurr = 0;
        int starMax = 0;
        bool isStarMaxInit = false;

        protected UIB_UIN_EquipInfoStar ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_EquipInfoStar();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.buttonRight.onClick.AddListener(this.OnClickRight);
            this.ui.buttonLeft.onClick.AddListener(this.OnClickLeft);
            this.ui.btnUpStar.onClick.AddListener(this.OnClickUp);
            this.ui.toggleMaxStarPreview.onValueChanged.AddListener(this.OnValChangeMaxStarPreview);

            this.equip = this.InitChildNode<UIN_CommonEquip>(this.ui.commonEquip);
            this.equip.SetCallClickView(this.OnClickEquipView);
            this.equip.SetViewActive(true);

            this.attrs = this.InitListScroll<UIN_CommonAttribute>(this.ui.attributeMould, this.OnAttrChanged);
            this.skillNameDescs = this.InitListScroll<UIN_CommonTitleValue>(this.ui.skillDes, this.OnSkillNameDescChanged);
            this.stars = this.InitListScroll<UIN_CommonDarkLightPreview>(this.ui.star, this.OnStarChanged);
            this.costMoney = this.InitChildNode<UIN_CostMoney>(this.ui.costMoney);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        #region 单击
        private void OnClickRight()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(true);
        }

        private UIEquipInfo GetBelongUI()
        {
            return this.belongWindow as UIEquipInfo;
        }

        private void OnClickLeft()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(false);
        }

        void OnClickEquipView(int index, UIN_CommonEquip item)
        {
            var ui = this.GetBelongUI();
            ui?.ShowEquipPicture();
        }

        void OnValChangeMaxStarPreview(bool isOn)
        {
            this.ViewPreview(isOn);
        }

        void OnClickUp()
        {
            if (!this.costMoney.CheckIsEnough())
            {
                return;
            }

            this.SendUpStar().Forget();
        }

        async UniTaskVoid SendUpStar()
        {
            var result = await EquipmentModule.NetCore.CS_upStar(this.udEquip.equipItem.id);
            if (result.IsSuccess)
            {
                this.udEquip.RefresEquipItems();
                this.RefreshCountAndIndex();
                this.Refresh();
            }
        }
        #endregion

        protected override void OnDispose()
        {
            this.equip = null;
            this.attrs = null;
            this.skillNameDescs = null;
            this.stars = null;
            this.costMoney = null;

            this.udEquip = null;
            this.equipIndex = 0;
            this.equipCount = 0;
        }

        public void Show(UD_EquipInfo udEquip)
        {
            this.Show();
            this.udEquip = udEquip;
            this.RefreshCountAndIndex();
            this.Refresh();
        }

        void RefreshCountAndIndex()
        {
            this.equipCount = this.udEquip.equipCount;
            this.equipIndex = this.udEquip.equipIndex;
        }

        void Refresh(ItemEquipmentData equipItem = null)
        {
            equipItem = equipItem == null ? this.udEquip.equipItem : equipItem;
            this.udEquip.ChangeEquip(equipItem);
            this.RefreshData();

            this.equip.Show(equipItem);
            this.ViewBtn();
            this.ViewAttribute();
            this.ViewSkilNameDesc();
            this.ViewStar();
            this.ViewCost();
            this.ViewMax();
        }

        void RefreshData()
        {
            var equipData = this.udEquip.equipItem;
            this.starInit = equipData.star;
            this.starCurr = this.starInit;
            this.starMax = equipData.maxStar;
            this.isStarMaxInit = this.starMax <= this.starInit;
        }

        void ViewBtn()
        {
            this.ui.buttonRight.SetActive(this.equipIndex < this.equipCount - 1);
            this.ui.buttonLeft.SetActive(this.equipIndex > 0);
            this.ui.toggleMaxStarPreview.SetActive(!this.isStarMaxInit);
            this.ui.btnUpStar.SetActive(!this.isStarMaxInit);
        }

        void ViewAttribute()
        {
            int cnt = this.udEquip.allAttributeCids.Count;
            this.attrs.RefillCells(cnt);
        }

        void OnAttrChanged(UIN_CommonAttribute item, int index)
        {
            var equipItem = this.udEquip.equipItem;
            int attrCid = this.udEquip.allAttributeCids[index];
            int attrValue = this.udEquip.GetAttributeValue(attrCid);
            int changNum = 0;
            if (!this.isStarMaxInit && this.starCurr == this.starMax)
            {
                changNum = AttributeDataUtils.GetEquipChangeAttributeByUpStar(equipItem, index, this.starCurr);
            }
            item.Show(attrCid, attrValue, changNum);
        }

        void ViewSkilNameDesc()
        {
            var equipItem = this.udEquip.equipItem;
            var cfgEquipStar = TableCenter.equipStar.GetCfg(equipItem.cid, this.starCurr);
            int cntSkil = cfgEquipStar.SkillIds.Count;
            this.skillNameDescs.RefillCells(cntSkil);
        }

        void OnSkillNameDescChanged(UIN_CommonTitleValue item, int index)
        {
            var equipItem = this.udEquip.equipItem;
            var cfgEquipStar = TableCenter.equipStar.GetCfg(equipItem.cid, this.starCurr);
            int skillCid = cfgEquipStar.SkillIds[index];
            var cfgSkillLv = TableCenter.skillLevel.GetCfg(skillCid, 1);
            var cfgSkill = TableCenter.skill.Get(skillCid);

            bool isUnlock = this.starInit >= (index + 1);
            int state = isUnlock ? 1 : 0;
            item.SetState(state);
            item.Show(cfgSkillLv.EffectText, cfgSkill.Name);
        }

        void ViewStar()
        {
            this.stars.RefillCells(this.starMax);
        }

        void OnStarChanged(UIN_CommonDarkLightPreview item, int index)
        {
            int star = (index + 1);
            int state = 0;
            if (this.starInit >= star)
                state = 1;
            else if (this.starInit < this.starCurr)
                state = 2;

            item.Show(state);
        }

        bool FilterCostItem(ItemData item)
        {
            if (item == null) return false;
            if (item is ItemEquipmentData itemEquip)
            {
                if (itemEquip.level <= 1 && itemEquip.star <= 1)
                    return (this.udEquip.equipItem != itemEquip);
            }
            return false;
        }

        void ViewCost()
        {
            if (this.isStarMaxInit)
            {
                this.ui.costTip.SetActive(false);
                this.costMoney.Hide();
                return;
            }
            var equipItem = this.udEquip.equipItem;
            var cfgEquipStar = equipItem.cfgStar;
            int kMoneyCid = -1;
            for (int i = 0, cnt = cfgEquipStar.CostItems.Count; i < cnt; i += APIUIEquip.OneCostItemCount)
            {
                var cfg = TableCenter.item.Get(cfgEquipStar.CostItems[i]);
                if (cfg != null)
                {
                    if (cfg.Type == CfgTable.ItemType.Basic)
                        kMoneyCid = i;
                }
            }
            if (kMoneyCid >= 0)
                this.costMoney.Show(cfgEquipStar.CostItems[kMoneyCid], cfgEquipStar.CostItems[kMoneyCid + 1]);
            else
                this.costMoney.Hide();

            bool isActiveCost = cfgEquipStar.CostBasicEquipNum > 0;
            this.ui.costTip.SetActive(isActiveCost);
            if (isActiveCost)
            {
                int itemCid = equipItem.cid;
                int itemNun = ItemModule.Instance.GetNum(itemCid, FilterCostItem);
                this.ui.textCost.text = $"{itemNun}/{cfgEquipStar.CostBasicEquipNum}";
            }
        }

        void ViewMax()
        {
            this.ui.starMax.SetActive(this.isStarMaxInit);
        }

        void ViewPreview(bool isPreview)
        {
            this.starCurr = isPreview ? this.starMax : this.starInit;
            this.ViewAttribute();
            this.ViewSkilNameDesc();
            this.ViewStar();
        }
    }
}
